package io.itit.ecp.admin.provide.action.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.provide.action.req.CarbonPriceQueryReq;
import io.itit.ecp.admin.provide.action.resp.CarbonPriceResp;
import io.itit.ecp.admin.server.entity.CarbonPriceEntity;
import io.itit.ecp.admin.server.entity.SceneProjectEntity;
import io.itit.ecp.admin.server.service.ICarbonPriceService;
import io.itit.grass.common.provide.action.req.BaseBatchIdReq;
import io.itit.grass.common.provide.action.req.BaseIdReq;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IAdd;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.common.provide.action.validation.IRequired;
import io.itit.grass.common.provide.action.validation.IUpdate;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 碳价配置管理
 */
@Api(tags = "碳价配置管理")
@RestController
@RequestMapping("/ecp-admin/carbonPrice")
public class CarbonPriceController {

    @Resource
    private ICarbonPriceService carbonPriceService;

    @ApiOperation("分页查询")
    @PostMapping("queryPage")
    @RequiresPermissions("ecp-admin:carbonPrice:search")
    public ResultResp<IPage<CarbonPriceResp>> queryPage(@RequestBody @Validated(IQueryPage.class) CarbonPriceQueryReq req) {
        Page<CarbonPriceResp> page = buildQW(req).joinPage(PageUtils.getPage(req, CarbonPriceResp.class), CarbonPriceResp.class);
        return ResultResp.success(page);
    }


    @ApiOperation("新增")
    @PostMapping("add")
    @RequiresPermissions("ecp-admin:carbonPrice:add")
    public ResultResp<Boolean> add(@RequestBody @Validated({IAdd.class}) CarbonPriceEntity req) {
        boolean res = carbonPriceService.save(req);
        return ResultResp.success(res);
    }

    @ApiOperation("编辑")
    @PostMapping("update")
    @RequiresPermissions("ecp-admin:carbonPrice:update")
    public ResultResp<Boolean> update(@RequestBody @Validated({IUpdate.class}) CarbonPriceEntity req) {
        boolean res = carbonPriceService.updateById(req);
        return ResultResp.success(res);
    }

    @ApiOperation("删除(根据Id删)")
    @PostMapping("deleteById")
    @RequiresPermissions("ecp-admin:carbonPrice:deleteById")
    public ResultResp<Boolean> deleteById(@RequestBody @Validated({IRequired.class}) BaseIdReq req) {
        return ResultResp.success(carbonPriceService.removeById(req.getId()));
    }

    @ApiOperation("批量删除")
    @PostMapping("batchDelete")
    @RequiresPermissions("ecp-admin:carbonPrice:batchDelete")
    public ResultResp<Boolean> batchDelete(@RequestBody @Validated({IRequired.class}) BaseBatchIdReq req) {
        return ResultResp.success(carbonPriceService.removeBatchByIds(req.getIds(), 1000));
    }

    private static JoinLambdaWrapper<CarbonPriceEntity> buildQW(CarbonPriceQueryReq req) {
        return Joins.of(CarbonPriceEntity.class)
                .leftJoin(SceneProjectEntity.class, SceneProjectEntity::getId, CarbonPriceEntity::getSceneProjectId)
                .selectAs(SceneProjectEntity::getProjectName, CarbonPriceResp::getProjectName)
                .end()
                .eq(StringUtils.hasLength(req.getSceneProjectId()), CarbonPriceEntity::getSceneProjectId, req.getSceneProjectId())
                .orderByDesc(StringUtils.hasLength(req.getSortColumn()), CarbonPriceEntity::getCreateTm);
    }

}
